package com.hzj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzj.controller.utils.IPUtils;
import com.hzj.controller.utils.RedisUtil;
import com.hzj.exception.HospitalException;
import com.hzj.helper.JwtHelper;
import com.hzj.pojo.UserInfo;
import com.hzj.mapper.UserInfoMapper;
import com.hzj.pojo.UserLoginRecord;
import com.hzj.pojo.vo.user.LoginVo;
import com.hzj.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.service.UserLoginRecordService;
import com.hzj.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author hzj
 * @since 2021-05-08
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    UserLoginRecordService ulrs;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    UserInfoMapper uim;

    @Override
    public HashMap<String, Object> login(LoginVo loginVo, HttpServletRequest request) {

//        获取ip地址
        String ipAddr = IPUtils.getIpAddr(request);
        loginVo.setIp(ipAddr);

        redisUtil.set("ip", ipAddr);

        String phone = loginVo.getPhone();
        String code = loginVo.getCode();
        //校验参数
        if (StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(code)) {
            throw new HospitalException(ResultCodeEnum.PARAM_ERROR);
        }

        //TODO 校验校验验证码
        String password = (String) redisUtil.get("userCode|" + loginVo.getPhone());
        if (!loginVo.getCode().equals(password)) {
            throw new HospitalException(ResultCodeEnum.CODE_ERROR);
        }

        //判断手机号是否已被使用
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        //获取会员
        UserInfo userInfo = uim.selectOne(queryWrapper);
        if (null == userInfo) {
            userInfo = new UserInfo();
            userInfo.setName("");
            userInfo.setPhone(phone);
            userInfo.setStatus(1);
            this.save(userInfo);
            userInfo = uim.selectOne(queryWrapper);
        }

//        //将用户信息存入session
//        request.getSession().setAttribute("userInfo", userInfo);
        redisUtil.set("userInfo" + IPUtils.getIpAddr(request), userInfo, 3600*24);
//        System.out.println("session登录id:" + request.getSession().getId());

        //校验是否被禁用
        if (userInfo.getStatus() == 0) {
            throw new HospitalException(ResultCodeEnum.LOGIN_DISABLED_ERROR);
        }

        //TODO 记录登录
        if (null != userInfo) {
            UserLoginRecord userLoginRecord = new UserLoginRecord();
            userLoginRecord.setUserId(userInfo.getId());
            userLoginRecord.setIp(loginVo.getIp());
            boolean save = ulrs.save(userLoginRecord);

        }


        //返回页面显示名称
        HashMap<String, Object> map = new HashMap<>();
        String name = userInfo.getName();
        if (StringUtils.isEmpty(name)) {
            name = userInfo.getNickName();
        }
        if (StringUtils.isEmpty(name)) {
            name = userInfo.getPhone();
        }
        map.put("name", name);
        //jwt生成token字符串
        String token = JwtHelper.createToken(userInfo.getId(), name);
        map.put("token", token);
        map.put("userInfo", userInfo);
        return map;
    }
}
